import 'package:flutter/material.dart';
import 'package:ichinesereader/constants.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/controller/books.dart';
import 'package:ichinesereader/models/book/BookSearchDto.dart';
import 'package:ichinesereader/utils/log.dart';

import '../../themes/appTheme.dart';

class SimpleBookSearchDialogue extends StatelessWidget {
  SimpleBookSearchDialogue({
    Key? key,
    required this.readingType,
  }) : super(key: key);
  final NanhaiBookReadingTypes readingType;
  final BooksController _booksController = Get.find();

  @override
  Widget build(BuildContext context) {
    Log.debug('build common SearchBooksPage');
    TextEditingController searchTextController = new TextEditingController();

    return AlertDialog(
      title: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            'Search Books',
            style: TextStyle(
                fontSize: 30.0,
                decoration: TextDecoration.underline,
                decorationStyle: TextDecorationStyle.dotted),
          ),
          InkWell(
            onTap: () {
              Navigator.of(context).pop();
            },
            child: Icon(Icons.cancel,
                color: Get.isDarkMode ? Colors.white : Colors.blueGrey),
          ),
        ],
      ),
      titleTextStyle: TextStyle(
          letterSpacing: 1,
          color: Get.isDarkMode ? Colors.white : Colors.blueGrey,
          fontSize: 21),
      backgroundColor:
          Get.isDarkMode ? AppTheme.colors.darkTheme2 : Colors.white,
      shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(6.0))),
      content: Container(
        width: 450.0,
        height: 300.0,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            TextFormField(
              controller: searchTextController,
              onFieldSubmitted: (query) {
                if (query.trim() != '') {
                  if (this.readingType == NanhaiBookReadingTypes.theme) {
                    BookSearchDto dto = _booksController.getBookSearchDeto();
                    dto.query = query;
                    _booksController.fetchProgramThemeBookItems(20, 0, dto);
                  }
                  Navigator.of(context).pop();
                }
              },
              decoration: InputDecoration(
                hintStyle: TextStyle(
                  color: Get.isDarkMode
                      ? Get.theme.primaryColor
                      : Colors.grey[300],
                ),
                prefixIcon: Icon(Icons.search),
                suffixIcon: IconButton(
                  icon: Icon(Icons.clear),
                  onPressed: () {
                    searchTextController.text = '';
                    if (this.readingType == NanhaiBookReadingTypes.theme) {
                      BookSearchDto dto = _booksController.getBookSearchDeto();
                      _booksController.fetchProgramThemeBookItems(20, 0, dto);
                    }
                  },
                ),
                hintText: 'Search Books...'.tr,
                border: OutlineInputBorder(),
              ),
            ),
            Center(
              child: Container(
                width: 210.0,
                height: 45.0,
                child: MaterialButton(
                  color: Get.isDarkMode
                      ? Get.theme.primaryColorDark
                      : Get.theme.primaryColor,
                  textColor: Colors.black,
                  onPressed: () {
                    var query = searchTextController.text;
                    if (query.trim() != '') {
                      if (this.readingType == NanhaiBookReadingTypes.theme) {
                        BookSearchDto dto =
                            _booksController.getBookSearchDeto();
                        dto.query = query;
                        _booksController.fetchProgramThemeBookItems(20, 0, dto);
                      }
                      Navigator.of(context).pop();
                    }
                  },
                  child: Text(
                    'Search'.tr,
                    style: TextStyle(
                      fontSize: 18.0,
                      color: Get.isDarkMode
                          ? Get.theme.primaryColor
                          : Get.theme.primaryColorLight,
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
